package com.tzpay.tzpay.ui.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lkl.cloudpos.aidl.AidlDeviceService;
import com.lkl.cloudpos.aidl.printer.AidlPrinter;
import com.lkl.cloudpos.aidl.printer.AidlPrinterListener;
import com.lkl.cloudpos.aidl.printer.PrintItemObj;
import com.lkl.cloudpos.aidl.printer.PrintItemObj.ALIGN;
import com.tzpay.tzpay.R;
import com.tzpay.tzpay.ui.BaseActivity;
import com.tzpay.tzpay.utils.T;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description:
 * Created:      [16/3/18]
 * Author:       [Yang]
 **/
public class TradeResultActivity extends BaseActivity {

    public static final String LKL_SERVICE_ACTION = "lkl_cloudpos_mid_service";

    @Bind(R.id.btn_back)
    TextView btnBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.btn_right)
    TextView btnRight;
    @Bind(R.id.btn_right_text)
    TextView btnRightText;
    @Bind(R.id.layout_title1)
    LinearLayout layoutTitle1;
    @Bind(R.id.result_icon)
    ImageView resultIcon;
    @Bind(R.id.result_message)
    TextView resultMessage;
    @Bind(R.id.result_btn)
    Button resultBtn;
    @Bind(R.id.error_msg)
    TextView errorMsg;

    boolean tradeResult;
    String tradeMessage;

    private AidlPrinter printerDev;

    @Override
    public void setupContentView() {
        setContentView(R.layout.activity_trade_result);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        changeTitle("交易结果");
        tradeResult = getIntent().getBooleanExtra("RESULT",false);
        tradeMessage = getIntent().getStringExtra("MESSAGE");

        if(tradeResult){
            resultIcon.setImageResource(R.drawable.silupay_correct);
            resultMessage.setText("交易成功");
        }else{
            resultIcon.setImageResource(R.drawable.silupay_error);
            resultMessage.setText("交易失败");
            errorMsg.setText(tradeMessage);
        }

        resultIcon.post(new Runnable() {
            @Override
            public void run() {
                printTax();
            }
        });
    }

    public void printTax(){
        try {
            printerDev.printText(new ArrayList<PrintItemObj>(){
                {
                    add(new PrintItemObj("商户名称(MERCHANT NAME):",8,false,ALIGN.LEFT));
                    add(new PrintItemObj(""));
                    add(new PrintItemObj("商户编码(MERCHANT NO):",8,false,ALIGN.LEFT));
                    add(new PrintItemObj(""));
                    add(new PrintItemObj("交易流水号(TRADE NO):",8,false,ALIGN.LEFT));
                    add(new PrintItemObj("",24));
                    add(new PrintItemObj("微信流水号(TRANSACTION NO):",8,true,ALIGN.LEFT));
                    add(new PrintItemObj("",8,true));
                    add(new PrintItemObj("金额(AMOUNT):",8,false,ALIGN.LEFT));
                    add(new PrintItemObj("RMB"+"",24,false,ALIGN.LEFT));
                    add(new PrintItemObj("交易时间(DATE/TIME):",8,false,ALIGN.LEFT));
                    add(new PrintItemObj("",8,false,ALIGN.LEFT));

                    add(new PrintItemObj("打印数据居中对齐测试",8,false,ALIGN.CENTER));
                    add(new PrintItemObj("打印数据居中对齐测试",8,false,ALIGN.CENTER));
                    add(new PrintItemObj("打印数据右对齐测试",8,false,ALIGN.RIGHT));
                    add(new PrintItemObj("打印数据右对齐测试",8,false,ALIGN.RIGHT));
                    add(new PrintItemObj("打印数据右对齐测试",8,false,ALIGN.RIGHT));
                    add(new PrintItemObj("打印数据下划线",8,false,ALIGN.LEFT,true));
                    add(new PrintItemObj("打印数据下划线",8,false,ALIGN.LEFT,true));
                    add(new PrintItemObj("打印数据下划线",8,false,ALIGN.LEFT,true));
                    add(new PrintItemObj("打印数据不换行测试",8,false,ALIGN.LEFT,false,false));
                    add(new PrintItemObj("打印数据不换行测试",8,false,ALIGN.LEFT,false,false));
                    add(new PrintItemObj("打印数据不换行测试",8,false,ALIGN.LEFT,false,false));
                    add(new PrintItemObj("打印数据行间距测试",8,false,ALIGN.LEFT,false,true,40));
                    add(new PrintItemObj("打印数据行间距测试",8,false,ALIGN.LEFT,false,true,40));
                    add(new PrintItemObj("打印数据行间距测试",8,false,ALIGN.LEFT,false,true,40));
                    add(new PrintItemObj("打印数据字符间距测试",8,false,ALIGN.LEFT,false,true,29,25));
                    add(new PrintItemObj("打印数据字符间距测试",8,false,ALIGN.LEFT,false,true,29,25));
                    add(new PrintItemObj("打印数据字符间距测试",8,false,ALIGN.LEFT,false,true,29,25));
                    add(new PrintItemObj("打印数据左边距测试",8,false,ALIGN.LEFT,false,true,29,0,40));
                    add(new PrintItemObj("打印数据左边距测试",8,false,ALIGN.LEFT,false,true,29,0,40));
                    add(new PrintItemObj("打印数据左边距测试",8,false, ALIGN.LEFT,false,true,29,0,40));
                }
            }, new AidlPrinterListener.Stub() {

                @Override
                public void onPrintFinish() throws RemoteException {
                    T.i("打印完成");
                }

                @Override
                public void onError(int arg0) throws RemoteException {
                    T.i("打印出错，错误码为：" + arg0);
                }
            });
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        bindService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }

    @OnClick(R.id.result_btn)
    protected void onClickResult() {
        finish();
    }

    public void bindService(){
        Intent intent = new Intent();
        intent.setAction(LKL_SERVICE_ACTION);
        boolean flag = bindService(intent, conn, Context.BIND_AUTO_CREATE);
        if(flag){
            T.d("服务绑定成功");
        }else{
            T.d("服务绑定失败");
        }
    }
    //设别服务连接桥
    private ServiceConnection conn = new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName name, IBinder serviceBinder) {
            T.d("aidlService服务连接成功");
            if(serviceBinder != null){	//绑定成功
                AidlDeviceService serviceManager = AidlDeviceService.Stub.asInterface(serviceBinder);
                onDeviceConnected(serviceManager);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            T.d("AidlService服务断开了");
        }
    };

    private void onDeviceConnected(AidlDeviceService serviceManager) {
        try {
            printerDev = AidlPrinter.Stub.asInterface(serviceManager.getPrinter());
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
